"use strict";

const {
  app: electronApp,
  dialog,
  shell,
} = require("electron");
const _ = require("lodash");

const Log = require("ee-core/log");

const { getConfig } = require('ee-core/config');

const { machineIdSync } = require("node-machine-id");
const crypto = require("crypto");


/**
 * 登录窗口功能控制器
 * @class
 */
class demoController {

  

  /**
   * 检测配置中http服务是否开启
   */
  async checkHttpServer() {
    const { enable, protocol, host, port } = getConfig().httpServer;
    const url = protocol + host + ':' + port;
    console.log('[checkHttpServer] url:', url);
    const data = {
      enable: enable,
      server: url
    }
    return data;
  }
  /**
   * 一个http请求访问此方法
   */
  async doHttpRequest() {
    const { CoreApp } = EE;
    // http方法
    const method = CoreApp.request.method;
    // http get 参数
    let params = CoreApp.request.query;
    params =
      params instanceof Object ? params : JSON.parse(JSON.stringify(params));
    // http post 参数
    const body = CoreApp.request.body;

    const httpInfo = {
      method,
      params,
      body,
    };
    Log.info("httpInfo:", httpInfo);

    if (!body.id) {
      return false;
    }
    const dir = electronApp.getPath(body.id);
    shell.openPath(dir);

    return true;
  }

  /**
   * 选择文件
   */
  selectFile() {
    const filePaths = dialog.showOpenDialogSync({
      properties: ["openFile"],
    });

    if (_.isEmpty(filePaths)) {
      return null;
    }

    return filePaths[0];
  }

  //初始化应用程序设置
  async getSettings() {
    const data = {
      result: null,
    };
    data.result = await JsondbService.getDataDir();
    return data;
  }
  //获取机器ID的Md5值
  getMachineId() {
    let code = machineIdSync();
    code = crypto.createHash("md5").update(code).digest("hex");
    return code;
  }

}

demoController.toString = () => "[class demoController]";
module.exports = demoController;
